from fastapi import FastAPI
import uvicorn
from contextlib import asynccontextmanager
from user import users
from chat import chat
from databases import engine
from models import Base
from auth import auth
from middleware import mymiddleware


@asynccontextmanager
async def lifespan(app: FastAPI):
    print("应用启动之前")
    Base.metadata.create_all(bind=engine)
    """
    初始化数据库
    """
    yield
    print("应用关闭之前")

app = FastAPI(
    title="Fastapi实时聊天系统",
    description="实现多用户实时聊天。",
    version="v1",
    docs_url="/",
    redoc_url= None,
    openapi_url="/swagger",
    lifespan=lifespan,
)



app.include_router(auth, prefix="/api/auth", tags=["认证授权"])
app.include_router(users, prefix="/api/user", tags=["用户管理"])
app.include_router(chat, prefix="/api/chat", tags=["聊天室管理"])


mymiddleware(app) # 注册中间件
@app.get("/test")
async def test():
    return {"message": "运行成功"}

if __name__ == "__main__":
    uvicorn.run(app=app, host="0.0.0.0", port=8000)